﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="Base.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Job
{
    using System;
    using System.Configuration;

    using Dhgms.TplHelper.Model.Info.JobResult;

    using NLog;

    /// <summary>
    /// Base class for the job handler
    /// </summary>
    /// <typeparam name="TJobResultClass">
    /// The Job Result Type
    /// </typeparam>
    /// <typeparam name="TJobSettings">
    /// The Job Settings Type
    /// </typeparam>
    public abstract class Base<TJobResultClass, TJobSettings>
        where TJobResultClass : Base, new() where TJobSettings : ConfigurationSection
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Base{TJobResultClass,TJobSettings}"/> class. 
        /// </summary>
        /// <param name="guid">
        /// Unique ID for the Job
        /// </param>
        /// <param name="friendlyName">
        /// Friendly Name for the Job
        /// </param>
        protected Base(Guid guid, string friendlyName)
        {
            if (guid == null || guid.Equals(Guid.Empty))
            {
                throw new ArgumentNullException("guid");
            }

            if (string.IsNullOrWhiteSpace(friendlyName))
            {
                throw new ArgumentNullException("friendlyName");
            }

            this.Guid = guid;
            this.FriendlyName = friendlyName;

            this.Logger = LogManager.GetLogger("Job");
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the friendly Name for the Job
        /// </summary>
        public string FriendlyName { get; private set; }

        /// <summary>
        /// Gets the Unique ID for the Job
        /// </summary>
        public Guid Guid { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Entry point for Job Execution
        /// </summary>
        /// <param name="settings">
        /// Settings for the job.
        /// </param>
        public TJobResultClass Execute(TJobSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            return this.OnExecute(settings);
        }

        /// <summary>
        /// The on execute event, where the donkey work should be done
        /// </summary>
        /// <param name="applicationSettings">The application settings</param>
        /// <returns>Job Result Data</returns>
        protected abstract TJobResultClass OnExecute(TJobSettings applicationSettings);

        #endregion

        /// <summary>
        /// Gets the NLog Logger
        /// </summary>
        protected Logger Logger { get; private set; }
    }
}